package com.phoenix.jsef.common;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @version V1.0
 * @ClassName: ListPageUtil
 * @Description:TODO(将list集合进行分页)
 * @author: 李启联
 * @date: 2018-09-28 17:22
 * @Copyright: Copyright (c), 2018-2018
 * 申明：本内容未经作者本人同意,禁止外泄以及用于其他的商业项目.
 */
public class ListPageUtil<T> {

    /**原集合**/
    private List<T> data;

    /** 上一页 **/
    private int prevPage;


    /** 当前页 **/
    private int pageIndex;

    /** 下一页 **/
    private int nextPage;

    /** 每页条数 **/
    private int pageSize;

    /** 总页数 **/
    private int totalPage;

    /** 总数据条数 **/
    private int totalCount;

    public ListPageUtil(List<T> data,int pageIndex,int pageSize) {
        if (data == null || data.isEmpty()) {
            throw new IllegalArgumentException("data must be not empty!");
        }

        this.data = data;
        this.pageSize = pageSize;

        this.pageIndex = pageIndex;
        this.totalCount = data.size();
        this.totalPage = (totalCount + pageSize - 1) / pageSize;
        this.prevPage = pageIndex-1>1? pageIndex-1:1;
        this.nextPage = pageIndex>=totalPage? totalPage: pageIndex + 1;

    }

    /**
     * 得到分页后的数据
     *
     * @return 分页后结果
     */
    public List<T> getPagedList() {
        int fromIndex = (pageIndex - 1) * pageSize;
        if (fromIndex >= data.size()) {
            return Collections.emptyList();//空数组
        }
        if(fromIndex<0){
            return Collections.emptyList();//空数组
        }
        int toIndex = pageIndex * pageSize;
        if (toIndex >= data.size()) {
            toIndex = data.size();
        }
        return data.subList(fromIndex, toIndex);
    }

    public int getPageSize() {
        return pageSize;
    }

    public List<T> getData() {
        return data;
    }
    public int getPrevPage() {
        return prevPage;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public int getNextPage() {
        return nextPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public static void main(String[] args) {
        Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
        List<Integer> list = Arrays.asList(array);

        // ListPageUtil<Integer> pager = new ListPageUtil<Integer>(list, 10);
        ListPageUtil<Integer> listPageUtil = new ListPageUtil<Integer>(list,2, 2);
        List<Integer> pagedList = listPageUtil.getPagedList();
        System.out.println(pagedList);
    }


}